unit uMotivoRefugo;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, FMTBcd, DB, DBClient, Provider, SqlExpr, StdCtrls, Buttons,
  ExtCtrls, Grids, DBGrids, rpcompobase, rpvclreport;

type
  TfMotivoRefugo = class(TForm)
    Panel1: TPanel;
    DBGrid2: TDBGrid;
    BitBtn7: TBitBtn;
    BitBtn5: TBitBtn;
    sqlMotivo: TSQLDataSet;
    dspMotivo: TDataSetProvider;
    cdsMotivo: TClientDataSet;
    dsMotivo: TDataSource;
    sqlMotivoTBL_CODTAB: TBCDField;
    sqlMotivoTBL_TIPTAB: TBCDField;
    sqlMotivoTBL_DESCRI: TStringField;
    cdsMotivoTBL_CODTAB: TBCDField;
    cdsMotivoTBL_DESCRI: TStringField;
    BitBtn1: TBitBtn;
    Label1: TLabel;
    sqlATS: TSQLDataSet;
    sqlATSTBA_CODTAB: TBCDField;
    sqlATSTBA_TIPTAB: TBCDField;
    sqlATSTBA_DESCRI: TStringField;
    cdsMotivoTBL_TIPTAB: TBCDField;
    DBGrid1: TDBGrid;
    Label2: TLabel;
    Label3: TLabel;
    dspAts: TDataSetProvider;
    cdsAts: TClientDataSet;
    dsAts: TDataSource;
    cdsAtsTBA_CODTAB: TBCDField;
    cdsAtsTBA_TIPTAB: TBCDField;
    cdsAtsTBA_DESCRI: TStringField;
    BitBtn2: TBitBtn;
    procedure FormShow(Sender: TObject);
    procedure BitBtn7Click(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
    procedure BitBtn5Click(Sender: TObject);
    procedure DBGrid2CellClick(Column: TColumn);
    procedure BitBtn2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  fMotivoRefugo: TfMotivoRefugo;

implementation

uses udm;

{$R *.dfm}

procedure TfMotivoRefugo.FormShow(Sender: TObject);
begin
  if (not cdsMotivo.Active) then
    cdsMotivo.Open;

  if (not cdsAts.Active) then
    cdsAts.Open;

  Label1.Caption := IntToStr(cdsAts.RecordCount) + ' Registros selecionados';
end;

procedure TfMotivoRefugo.BitBtn7Click(Sender: TObject);
var rep1: TVCLReport;
  i : Integer;
  str: string;
begin
{  i := 0;
  with DBGrid2.DataSource.DataSet do
  begin
    While (i < DBGrid2.SelectedRows.Count) do
    begin
      GotoBookmark(Pointer(DBGrid2.SelectedRows.Items[i]));
      str := 'INSERT INTO ATS_TABELAS (TBA_TIPTAB,' +
        ' TBA_CODTAB, TBA_DESCRI) VALUES(' +
        IntToStr(cdsMotivoTBL_TIPTAB.AsInteger) + ', ' +
        IntToStr(cdsMotivoTBL_CODTAB.asInteger) + ', ' +
        QuotedStr(cdsMotivoTBL_DESCRI.AsString) + ')';
      dm.SQLConnection1.ExecuteDirect(str);
      i := i + 1;
    end;
  end;                           }
  rep1 := TVCLReport.Create(nil);
  rep1.PrinterSetup;
  rep1.FileName := 'lista_motivos.rep';
  rep1.Report.DatabaseInfo.Items[0].SQLConnection := dm.SQLConnection1;
  Rep1.Title := Rep1.FileName;
  Rep1.Execute;
  Rep1.Destroy;
end;

procedure TfMotivoRefugo.BitBtn1Click(Sender: TObject);
begin
  dm.SQLConnection1.ExecuteDirect('DELETE FROM ATS_TABELAS');
  if (cdsAts.Active) then
    cdsAts.Close;
  cdsAts.Open;
  Label1.Caption := IntToStr(cdsAts.RecordCount) + ' Registros selecionados';
end;

procedure TfMotivoRefugo.BitBtn5Click(Sender: TObject);
begin
  Close;
end;

procedure TfMotivoRefugo.DBGrid2CellClick(Column: TColumn);
var str:string;
begin
  if (not cdsAts.Active) then
    cdsAts.Open;

  if (not cdsAts.Locate('TBA_CODTAB', cdsMotivoTBL_CODTAB.asInteger, [loCaseInsensitive])) then
  begin
    str := 'INSERT INTO ATS_TABELAS (TBA_TIPTAB,' +
      ' TBA_CODTAB, TBA_DESCRI) VALUES(' +
      IntToStr(cdsMotivoTBL_TIPTAB.AsInteger) + ', ' +
      IntToStr(cdsMotivoTBL_CODTAB.asInteger) + ', ' +
      QuotedStr(cdsMotivoTBL_DESCRI.AsString) + ')';
    dm.SQLConnection1.ExecuteDirect(str);
    if (cdsAts.Active) then
      cdsAts.Close;
    cdsAts.Open;
  end;

  Label1.Caption := IntToStr(cdsAts.RecordCount) + ' Registros selecionados';
end;

procedure TfMotivoRefugo.BitBtn2Click(Sender: TObject);
begin
  begin
    dm.SQLConnection1.ExecuteDirect('DELETE FROM ATS_TABELAS WHERE TBA_CODTAB = ' +
      intToStr(cdsAtsTBA_CODTAB.asInteger));
    if (cdsAts.Active) then
      cdsAts.Close;
    cdsAts.Open;
    Label1.Caption := IntToStr(cdsAts.RecordCount) + ' Registros selecionados';
  end;

end;

end.
